# -*- coding: utf-8 -*-
import os
import time
import datetime
import email
import mimetypes
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
import smtplib

def Find(path):
    temptime = None
    
    for item in os.listdir(path):
        if os.path.isfile(os.path.join(path, item)):
            filename = os.path.join(path, item)
            filetime = time.localtime(os.path.getmtime(filename))

##            print time.strftime('%Y-%m-%d %H:%M:%S', filetime)
            if temptime is None or filetime > temptime:
                temptime = filetime 
            
        if os.path.isdir(os.path.join(path, item)):
            result = Find(os.path.join(path, item))

            if result is not None:
                if temptime is not None:
                    if result > temptime:
                        temptime =  result
                else:
                    temptime =  result
                    
    return temptime

def SendMail(authinfo, fromAddr, toAddr, subject, plaintext, htmltext):
    server = authinfo.get('server')
    user = authinfo.get('user')
    pwd = authinfo.get('password')

    if not (server and user and pwd):
        print 'incomplete login info, exit now'
        return

    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot['From'] = fromAddr
    msgRoot['To'] = toAddr

    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    msgText = MIMEText(plainText, 'plain', 'utf-8')
    msgAlternative.attach(msgText)



    smtp = smtplib.SMTP()
    smtp.set_debuglevel(1)
    smtp.connect(server)
    smtp.login(user, pwd)
    smtp.sendmail(fromAddr, toAddr, msgRoot.as_string())
    smtp.quit()

    
if __name__ == "__main__":
    currentTime = time.localtime()
    f = open('d:\\agent lead\\monitor.log', 'a')
    
    latestfiletime = Find('D:\\Agent Lead\\Site\\download\\Report')
    
    f.write('Max time is ' + time.strftime('%Y-%m-%d %H:%M:%S', latestfiletime) + '\n')
    f.write('Current time is ' + time.strftime('%Y-%m-%d %H:%M:%S', currentTime) + '\n')

    date1 = datetime.datetime(latestfiletime.tm_year, latestfiletime.tm_mon, latestfiletime.tm_mday,
                              latestfiletime.tm_hour, latestfiletime.tm_min, latestfiletime.tm_sec)

    date2 = datetime.datetime(currentTime.tm_year, currentTime.tm_mon, currentTime.tm_mday,
                              currentTime.tm_hour, currentTime.tm_min, currentTime.tm_sec)

    diffminutes = (date2-date1).total_seconds() / 60
    f.write(str(diffminutes) + '\n')
    
    if diffminutes > 10.0:
        f.write('The application was stoped\n')

        authinfo = {}
        authinfo['server'] = 'smtp.163.com'
        authinfo['user'] = 'limao4223127@163.com'
        authinfo['password'] = '4223127666'

        fromAddr = 'limao4223127@163.com'
        toAddr = 'limao4223127@hotmail.com'
        subject = 'alert'
        plainText = 'The application was stoped'

        SendMail(authinfo, fromAddr, toAddr, subject, plainText, '')

    f.close()

